package com.dnzx.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.dnzx.framework.orm.mybatis.MyBatisBaseServiceImpl;
import org.dnzx.framework.orm.mybatis.MybatisBaseDao;
import org.dnzx.framework.page.PageResult;
import org.springframework.stereotype.Service;

import com.dnzx.dao.IRoleMenuDao;
import com.dnzx.model.system.RoleMenuBO;
import com.dnzx.service.IRoleMenuService;


/**
 * <p>系统后台角色资源serivce实现类 </p>
 *
 * @author Steven
 * @version V1.0
 * @Package com.dnzx.service.impl
 * 
 * @date 2017/9/19 22:28
 */
@Service
public class RoleMenuServiceImpl extends MyBatisBaseServiceImpl<RoleMenuBO,Long> implements IRoleMenuService {

	@Resource
	private IRoleMenuDao roleMenuDao;
	    
	@Override
	public MybatisBaseDao<RoleMenuBO, Long> getBaseDAO() {
		return this.roleMenuDao;
	}

	@Override
	public void save(RoleMenuBO roleMenuBO) {
		// 要先删除之前的配置，再保存新的配置
		this.roleMenuDao.deleteByRoleId(roleMenuBO.getRoleId());
		List<Long> menuIdList = roleMenuBO.getMenuIdList();
		if (menuIdList != null && menuIdList.size() > 0) {
			RoleMenuBO roleMenuBODb = null;
			for (int i = 0; i < menuIdList.size(); i++) {
				roleMenuBODb = new RoleMenuBO();
				roleMenuBODb.setRoleId(roleMenuBO.getRoleId());
				roleMenuBODb.setMenuId(menuIdList.get(i));
//				roleMenuBODb.setAddUser(1);// TODO 取当前登陆者
//				roleMenuBODb.setModUser(1);// TODO 取当前登陆者
				this.roleMenuDao.save(roleMenuBODb);
			}
		}
	}
	
	@Override
	public void delete(Long id) {
		this.roleMenuDao.delete(id);
	}
	
	@Override
	public void update(RoleMenuBO roleMenuBO) {
		this.roleMenuDao.update(roleMenuBO);
	}
	    
	@Override
	public RoleMenuBO get(Long id) {
		RoleMenuBO roleMenuBO = this.roleMenuDao.get(id);
		return roleMenuBO;
	}
	    
	@Override
	public List<RoleMenuBO> queryList(RoleMenuBO roleMenuBO) {	
		return this.roleMenuDao.queryList(roleMenuBO);
	}

	@Override
	public PageResult<List<RoleMenuBO>> queryPage(RoleMenuBO roleMenuBO, PageResult<List<RoleMenuBO>> pageResult) {
		int totalRows = this.roleMenuDao.queryCount(roleMenuBO);		
		pageResult.setTotalRows(totalRows);
		pageResult.setCurrentPage(roleMenuBO.getCurrentPage());
        pageResult.setPageSize(roleMenuBO.getPageSize());
		List<RoleMenuBO> records = this.roleMenuDao.queryPage(pageResult.getPageSize(), pageResult.getStartRow(), roleMenuBO);
		pageResult.setT(records);
		return pageResult;
	}
	
	@Override
	public Map<Long, Long> queryMap(Long roleId) {
		RoleMenuBO roleMenuBOQuery = new RoleMenuBO();
		roleMenuBOQuery.setRoleId(roleId);
		List<RoleMenuBO> roleMenuBOList = this.roleMenuDao.queryList(roleMenuBOQuery);
		Map<Long, Long> map = new HashMap<Long, Long>();
		if (roleMenuBOList != null && roleMenuBOList.size() > 0) {
			for (RoleMenuBO roleMenuBOTemp : roleMenuBOList) {
				map.put(roleMenuBOTemp.getMenuId(), roleMenuBOTemp.getMenuId());
			}
		}
		return map;
	}
	
}
